﻿using System;
using System.Collections.Generic;
using System.Text;
using Business.Entities;
using System.Data;
using System.Data.SqlClient;

namespace Data.Database
{
  public class MateriaAdapter:Adapter
    {
        public List<Materia> GetAll()
        {

            List<Materia> materias = new List<Materia>();
            try
            {
                this.OpenConnection();
                SqlCommand cmdMaterias = new SqlCommand("select * from materias", SqlConn);
                SqlDataReader drMaterias = cmdMaterias.ExecuteReader();


                while (drMaterias.Read())
                {
                    Materia mtr = new Materia();
                    mtr.ID = (int)drMaterias["id_materia"];
                    mtr.Descripcion=(string)drMaterias["desc_materia"];
                    mtr.Hssemanales = (int)drMaterias["hs_semanales"];
                    mtr.Hstotales = (int)drMaterias["hs_totales"];
                    mtr.Idplan = (int)drMaterias["id_plan"];
                    materias.Add(mtr);

                }
                drMaterias.Close();
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar lista de Materias", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return materias;
        }
        
        public Business.Entities.Materia GetOne(int ID)
        {
            Materia mtr = new Materia();

            try
            {
                this.OpenConnection();
                SqlCommand cmdMaterias = new SqlCommand("select  * from materias where id_materia = @id", SqlConn);
                cmdMaterias.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                SqlDataReader drMaterias = cmdMaterias.ExecuteReader();
                if (drMaterias.Read())
                {
                    mtr.ID = (int)drMaterias["id_materia"];
                    mtr.Descripcion = (string)drMaterias["desc_materia"];
                    mtr.Hssemanales = (int)drMaterias["hs_semanales"];
                    mtr.Hstotales = (int)drMaterias["hs_totales"];
                    mtr.Idplan = (int)drMaterias["id_plan"];

                }
                drMaterias.Close();
                
                
            }
            catch (Exception Ex)
            {
                Exception ExcepcionManejada =
                new Exception("Error al recuperar datos de la materia", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

            return mtr;
        }


        public void Delete(int ID)
        {

            try
            {
                this.OpenConnection();
                SqlCommand cmdDelete = new SqlCommand("delete from materias where id_materia=@id", SqlConn);
                cmdDelete.Parameters.Add("@id", SqlDbType.Int).Value = ID;
                cmdDelete.ExecuteNonQuery();
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al eliminar materia", Ex);
                throw ExcepcionManejada;

            }

            finally
            {
                this.CloseConnection();
            }


        }


        protected void Update(Materia materia)
        {
            
            try
            {
                this.OpenConnection();
                SqlCommand cmdSave = new SqlCommand(
                "UPDATE materias set desc_materia=@desc_materia,hs_semanales=@hs_semanales, " +
                "hs_totales=@hs_totales, id_plan=@id_plan " +
                "WHERE id_materia=@id", SqlConn);
                cmdSave.Parameters.Add("@id", SqlDbType.Int).Value = materia.ID;
                cmdSave.Parameters.Add("@desc_materia", SqlDbType.VarChar, 50).Value = materia.Descripcion;
                cmdSave.Parameters.Add("@hs_semanales", SqlDbType.Int).Value = materia.Hssemanales;
                cmdSave.Parameters.Add("@hs_totales", SqlDbType.Int).Value = materia.Hstotales;
                cmdSave.Parameters.Add("@id_plan", SqlDbType.Int).Value = materia.Idplan;
                cmdSave.ExecuteNonQuery();
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al modificar datos del usuario", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

        }


        protected void Insert(Materia materia)
        {
            try
            {
                this.OpenConnection();
                SqlCommand cmdSave = new SqlCommand(
                "insert into materias(desc_materia,hs_semanales,hs_totales,id_plan) " +
                "values(@desc_materia,@hs_semanales,@hs_totales,@id_plan) " +
                "select @@identity", SqlConn);

                cmdSave.Parameters.Add("@id", SqlDbType.Int).Value = materia.ID;
                cmdSave.Parameters.Add("@desc_materia", SqlDbType.VarChar, 50).Value = materia.Descripcion;
                cmdSave.Parameters.Add("@hs_semanales", SqlDbType.Int).Value = materia.Hssemanales;
                cmdSave.Parameters.Add("@hs_totales", SqlDbType.Int).Value = materia.Hstotales;
                cmdSave.Parameters.Add("@id_plan", SqlDbType.Int).Value = materia.Idplan;

                materia.ID = Decimal.ToInt32((decimal)cmdSave.ExecuteScalar());
               
            }

            catch (Exception Ex)
            {
                Exception ExcepcionManejada = new Exception("Error al crear Materia", Ex);
                throw ExcepcionManejada;
            }
            finally
            {
                this.CloseConnection();
            }

        }





        public void Save(Materia mtr)
        {
            if (mtr.State==BussinessEntity.States.Deleted)
            {
                this.Delete(mtr.ID);
            }
            else if (mtr.State == BussinessEntity.States.New)
            {
                this.Insert(mtr);
            }
            else if (mtr.State == BussinessEntity.States.Modified)
            {
                this.Update(mtr);
            }
            mtr.State = BussinessEntity.States.Unmodified;

        }
    }
}
